//
//  OMPersonalCenterOrderViewController.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/20.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMPersonalCenterOrderViewController.h"
#import "OMOrderListViewController.h"
#import "OMOrderUnfinishedViewController.h"
#import "OMOrderFinishedViewController.h"
#import "OMOrderCanceledViewController.h"
#import "OMNoLoginView.h"
@interface OMPersonalCenterOrderViewController ()
@property (nonatomic, strong) OMNoLoginView *loginView;
@end

@implementation OMPersonalCenterOrderViewController

- (void)viewDidLoad {



    OMOrderListViewController *vc1 = [[OMOrderUnfinishedViewController alloc] init];
    vc1.title = @"未完成";
    vc1.orderStatus = OMOrderStatusUnfinished;

    OMOrderListViewController *vc2 = [[OMOrderFinishedViewController alloc] init];
    vc2.title = @"已完成";
    vc2.orderStatus = OMOrderStatusCompleted;

    OMOrderListViewController *vc3 = [OMOrderCanceledViewController new];
    vc3.title = @"已取消";
    vc3.orderStatus = OMOrderStatusCanceled;

    self.pagingView.bounces = NO;

    NSArray *viewCtrls = @[vc1,vc2,vc3];

    self.controllers = viewCtrls;

    [super viewDidLoad];


    self.loginView = [OMNoLoginView new];
    [self.view addSubview:self.loginView];
    [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    _loginView.hidden = true;
    self.sectionBar.backgroundColor = HEXCOLOR(@"fafafa");
    self.navigationItem.title = @"我的订单";
    // Do any additional setup after loading the view.
    
    @weakify(self);
    [SMNotificationMonitorRACSignal(OM_NOTIFICATION_KEY_LOGIN_SUCCESS) subscribeNext:^(id x) {
        @strongify(self);
        [self hideLoginView];
    }];

    [SMNotificationMonitorRACSignal(OM_NOTIFICATION_KEY_LOGOUT_SUCCESS) subscribeNext:^(id x) {
        @strongify(self);
        [self showLoginView];

    }];
}

- (void)showLoginView
{
    self.loginView.hidden = NO;
    [self.view bringSubviewToFront:self.loginView];
}

- (void)hideLoginView
{
    self.loginView.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (![Storage isLogin]) {
        [self showLoginView];
    }else {
        [(OMOrderListViewController*)self.controllers[self.selectedIndex] startAnimating:YES];
        [(OMOrderListViewController*)self.controllers[self.selectedIndex] reloadDataFromNetWork];
    }
}


- (void) receiveNotifications:(NSString *)notify
{
    @weakify(self);
    [SMNotificationMonitorRACSignal(notify) subscribeNext:^(NSNotification *notifiation) {
        @strongify(self);
        [self switchToPage:[notifiation.object unsignedIntegerValue] animated:NO];
    }];
}

@end
